package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public boolean saveWithDishFlaver(DishDto dishDto) {
        //1.先保存到dish表
        this.save(dishDto);
        //2.判断flavors集合是否有元素
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (flavors.size() != 0) {
            for (int i = 0; i < flavors.size(); i++) {
                //获取每一个dishFlavor对象
                DishFlavor dishFlavor = flavors.get(i);
                //给dishFlavor添加dishid
                dishFlavor.setDishId(dishDto.getId());
                dishFlavorService.save(dishFlavor);
            }
        }
        return true;
    }

    @Override
    public boolean updateWithDishFlavar(DishDto dishDto) {
        //1.先更新dish表
        this.updateById(dishDto);
        //2.更新口味表
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);//先删除所有相关的口味

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors); //再添加进去
        return true;
    }

    @Override
    public void delete(List<Long> ids) {
        //1.先判断当前菜品是否在售
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        int count = this.count(dlqw);
        if (count > 0) {
            throw new BusinessException("菜品处于在售状态，暂无法删除");
        }

        //判断含有菜品的套餐是否正在售
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(SetmealDish::getDishId, ids);
        List<SetmealDish> setmealDishList = setmealDishService.list(sdlqw);

        List<Long> setmealidList = setmealDishList.stream().map(setmealDish -> {
            Long setmealId = setmealDish.getSetmealId();
            return setmealId;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.in(Setmeal::getId, setmealidList).eq(Setmeal::getStatus, 1);
        int count1 = setmealService.count(slqw);
        if (count1 > 0) {
            throw new BusinessException("有套餐正在售卖该菜品,无法删除");
        }

        //2.删除菜品表
        this.removeByIds(ids);
        //3.再删除口味表
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(lqw);

        //4.删除套餐表里的该菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId, ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

}
